package com.example.fushuang.balabala.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.fushuang.balabala.R;
import com.example.fushuang.balabala.model.VideoDetail;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by admin on 2016/10/15.
 */
//58046ed0e0f55a71670000ec
public class AvRecycleAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    private List<VideoDetail.DataBean.RelatesBean> mList;
    private Context mContext;



    public AvRecycleAdapter(Context context, List<VideoDetail.DataBean.RelatesBean> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.activity_av_recycle_item,parent,false);
        SimpleViewHolder simpleViewHolder = new SimpleViewHolder(view);
        return simpleViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        SimpleViewHolder simpleViewHolder = (SimpleViewHolder) holder;
        VideoDetail.DataBean.RelatesBean bean = mList.get(position);
        simpleViewHolder.itemView.setOnClickListener(this);
        simpleViewHolder.itemView.setTag(bean);

        simpleViewHolder.bindView(bean);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.av_recycle_item:
                Toast.makeText(mContext, "item点击", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private static class SimpleViewHolder extends RecyclerView.ViewHolder {

        private SparseArrayCompat<View> mViews=new SparseArrayCompat<>(); //相当于一个高性能的map,用于储存id和控件,减少findById的次数

        public SimpleViewHolder(View itemView) {
            super(itemView);
        }
        public void bindView(VideoDetail.DataBean.RelatesBean relatesBean){
            ImageView imageView = (ImageView) getChildView(R.id.av_introduce_recycleView_item_img);
            TextView textView = (TextView) getChildView(R.id.av_introduce_recycleView_item_title);
            textView.setText(relatesBean.getTitle());
            Picasso.with(imageView.getContext())
                    .load(relatesBean.getPic())
                    .config(Bitmap.Config.RGB_565)
                    .resize(320, 200)
                    .into(imageView);
        }

        private View getChildView(int id) {
            View view = mViews.get(id);
            if (view==null) {
                view = itemView.findViewById(id);
                mViews.put(id,view);
            }
            return view;
        }


    }


}
